package com.cct.digitalarchapi.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cct.digitalarchapi.common.Response;
import com.cct.digitalarchapi.dao.ArchCategoryMapper;
import com.cct.digitalarchapi.dao.ArchDeptCategoryMapper;
import com.cct.digitalarchapi.dao.ArchDeptMapper;
import com.cct.digitalarchapi.pojo.ArchApplication;
import com.cct.digitalarchapi.pojo.ArchCategory;
import com.cct.digitalarchapi.pojo.ArchDept;
import com.cct.digitalarchapi.pojo.ArchDeptCategory;
import com.cct.digitalarchapi.pojo.vo.ArchDeptWithCateVo;
import com.cct.digitalarchapi.pojo.vo.DeptCategoryVo;
import com.cct.digitalarchapi.service.ArchDeptCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @FileName ArchCategoryServiceImpl
 * @Description 档案分类实现类
 * @Author Created by chenmingzhe
 * @Date 2023/11/19
 **/
@Slf4j
@Service
public class ArchDeptCategoryServiceImpl implements ArchDeptCategoryService {

    @Autowired
    private ArchDeptCategoryMapper archDeptCategoryMapper;
    @Autowired
    private ArchCategoryMapper archCategoryMapper;

    @Override
    public Response getAllDeptCategory(int pageNum,int pageSize,String archCateName,String archDeptName) {
        try{
            Page<DeptCategoryVo> page=new Page<>(pageNum,pageSize);
            archDeptCategoryMapper.getAllDeptCategory(page,archCateName,archDeptName);
            return Response.success(page);
        }catch (Exception e){
            e.printStackTrace();
            log.error("查询所有部门档案分类失败：{}",e.getMessage());
            return Response.error("查询所有部门档案分类失败");
        }

    }


    @Override
    public Response addDeptCategory(ArchDeptCategory archDeptCategory) {
        try{
            int num=archDeptCategoryMapper.qryHaveDeptCategory(archDeptCategory.getArchDeptCateCode());
            if(num>=1){
                return Response.bad("部门档案分类已存在！");
            }
            //新增默认可用
            archDeptCategory.setArchDeptClassType(0);
            archDeptCategoryMapper.addDeptCategory(archDeptCategory);
            return Response.success("");
        }catch (Exception e){
            e.printStackTrace();
            log.error("新增部门档案分类失败：{}",e.getMessage());
            return Response.error("新增部门档案分类失败");
        }
    }


    @Override
    public Response deleteDeptCategory(int archDeptCatId) {
        try {
            archDeptCategoryMapper.deleteDeptCategory(archDeptCatId);
            return Response.success("");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("删除档案分类失败：{}",e.getMessage());
            return Response.error("删除档案分类失败");
        }
    }

    @Override
    public Response updateDeptCategory(ArchDeptCategory archDeptCategory) {
        try {
            archDeptCategoryMapper.updateDeptCategory(archDeptCategory);
            return Response.success("");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("修改部门档案分类失败：{}",e.getMessage());
            return Response.error("修改部门档案分类失败");
        }
    }

    @Override
    public Response getCateByDept(String archDeptName) {
        try {
            List<DeptCategoryVo> list =archDeptCategoryMapper.getCateByDept(archDeptName);
            List<ArchCategory> res=new ArrayList<>();
            for (DeptCategoryVo deptCategoryVo : list) {
                ArchCategory archCategory=new ArchCategory();
                archCategory.setArchCateCode(deptCategoryVo.getArchCateCode());
                archCategory.setArchCateName(deptCategoryVo.getArchCateName());
                res.add(archCategory);
            }
            return Response.success(res);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询该部门的档案分类失败：{}",e.getMessage());
            return Response.error("查询该部门的档案分类失败");
        }
    }

    @Override
    public Response getDeptWithCate() {
        try {
            List<ArchDeptWithCateVo> list=archCategoryMapper.getAllCate();
            List<ArchDeptCategory> deptCategoryList=archDeptCategoryMapper.getAllDeptCate();
            for (ArchDeptWithCateVo archDeptWithCateVo : list) {
                archDeptWithCateVo.setFlag("0");
                List<ArchDeptCategory> archDeptCategoryList=new ArrayList<>();
                for (ArchDeptCategory archDeptCategory : deptCategoryList) {
                    if(archDeptCategory.getArchCateName().equals(archDeptWithCateVo.getArchCateName())){
                        archDeptCategoryList.add(archDeptCategory);
                    }
                }
                archDeptWithCateVo.setArchDeptCategoryList(archDeptCategoryList);
            }
            return Response.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询该部门档案分类失败：{}",e.getMessage());
            return Response.error("查询该部门档案分类失败");
        }
    }
}
